package com.teatime.client;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.teatime.client.view.TFrame;
import com.teatime.client.view.TFrame.OnTFrameListener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;

@SuppressLint("UseSparseArrays")
public class TFrameMgr implements 
			OnTFrameListener,
			AnimationListener{
	
	private Map<Integer, TFrame> mFrames;
	private Activity mActivity;
	private LayoutInflater mLayoutFactory;
	private TFrame mHome;
	private TFrame mCurrFrame;
	private TFrame mAnimFrame;
	private Animation mInAnimation;
	private Animation mOutAnimation;
	private boolean   mIsAnim;
	
	public TFrameMgr(Activity activity) {
		mIsAnim = false;
		mActivity = activity;
		mFrames = new HashMap<Integer, TFrame>();
		mInAnimation = mOutAnimation = null;
	}

	public TFrame create(int layoutId) 
			throws IllegalArgumentException{
		TFrame frame = get(layoutId);
		if (frame != null) {
			return frame;
		}
		
		if (mLayoutFactory == null) {
			mLayoutFactory = LayoutInflater.from(mActivity);
		}
		View view = mLayoutFactory.inflate(layoutId, null);
		if (view == null || !(view instanceof TFrame)) {
			throw new IllegalArgumentException("it is not a TFrame layout - " + layoutId);
		}
		frame = (TFrame)view;
		frame.onCreate();
		frame.setOnBackListener(this);
		mFrames.put(layoutId, frame);
		return frame;
	}
	
	public TFrame get(int layoutId) {
		return mFrames.get(layoutId);
	}
	
	private Animation getInAnimation() {
		if (mInAnimation == null) {
			mInAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.anim_pop_win_in);
			mInAnimation.setAnimationListener(this);
		}
		mInAnimation.setAnimationListener(this);
		return mInAnimation;
	}
	
	@SuppressWarnings("unused")
	private Animation getOutAnimation() {
		if (mOutAnimation == null) {
			mOutAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.anim_pop_win_out);
			mOutAnimation.setAnimationListener(this);
		}
		mOutAnimation.setAnimationListener(this);
		return mOutAnimation;
	}
	
	public void show(int layoutId, 
			boolean createIfNotExisits) 
			throws IllegalArgumentException {
		if (mIsAnim) {
			return;
		}
		
		TFrame frame = get(layoutId);
		if (frame == null && createIfNotExisits) {
			frame = create(layoutId);
		}
		if (frame != null) {
			mCurrFrame = frame;
			mActivity.setContentView(frame);
			/*if (mCurrFrame == null) {
				mCurrFrame = frame;
				TLog.v("TFrameMgr.show, set directly, mCurrFrame:" + mCurrFrame);
				mActivity.setContentView(mCurrFrame);
			}
			else {
				mAnimFrame = frame;
				TLog.v("TFrameMgr.show,startAnimation, mCurrFrame:" + mCurrFrame);
				ViewGroup.LayoutParams lp = 
						new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
								ViewGroup.LayoutParams.FILL_PARENT);
				mActivity.addContentView(mAnimFrame, lp);
				mAnimFrame.startAnimation(getInAnimation());
				//mActivity.setContentView(mCurrFrame);
			}*/
		}
	}
	
	public void onActivityResult(int requestCode, 
			int resultCode,
            Intent intent) {
		for (Iterator<?> it = mFrames.entrySet().iterator(); it.hasNext();it.next()) {
			Entry<?, ?> e = (Entry<?, ?>)it.next();
			if (e != null && e.getValue() != null) {
				TFrame frame = (TFrame)e.getValue();
				if (frame.onActivityResult(requestCode, resultCode, intent)) {
					break;
				}
			}
		}
	}
	
	public void setHomeFrame(TFrame home) {
		mHome = home;
	}
	
	public void setHomeFrame(int id) {
		mHome = get(id);
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		// TODO Auto-generated method stub
		if (animation == getInAnimation()) {
			if (mAnimFrame != null) {
				TLog.v("TFrameMgr.onAnimationEnd, in, mCurrFrame:" + mCurrFrame);
				mCurrFrame = mAnimFrame;
				mAnimFrame = null;
				mActivity.setContentView(mCurrFrame);
			}
		}
		else {
			TLog.v("TFrameMgr.onAnimationEnd, out, mCurrFrame:" + mCurrFrame);
			mAnimFrame = null;
			mActivity.setContentView(mCurrFrame);
		}
		
		mIsAnim = false;
		animation.reset();
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub
		mIsAnim = true;
		TLog.v("TFrameMgr.onAnimationStart, mCurrFrame:" + mCurrFrame);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (mCurrFrame != null) {
			return mCurrFrame.onKeyDown(keyCode, event);
		}
		return false;
	}

	@Override
	public void onTitleClicked(TFrame frame, boolean left) {
		// TODO Auto-generated method stub
		if (left) {
			onBackKeyDown(frame);
		}
	}

	@Override
	public void onBackKeyDown(TFrame frame) {
		// TODO Auto-generated method stub
		if (mHome != null && frame != mHome) {
			/*
			 * mAnimFrame = frame; mCurrFrame = mHome;
			 * mActivity.setContentView(mHome); ViewGroup.LayoutParams lp = new
			 * ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
			 * ViewGroup.LayoutParams.FILL_PARENT);
			 * mActivity.addContentView(mAnimFrame, lp);
			 * mAnimFrame.startAnimation(getOutAnimation());
			 */
			mCurrFrame = mHome;
			mActivity.setContentView(mHome);
			return;
		}
		if (frame == mHome) {
			mActivity.finish();
		}
		
	}
}
